import { request } from "../../utils/request.js";
import regeneratorRuntime from "../../utils/runtime.js";
var bsurl = require('../../utils/bsurl.js');
Page({
  data: {
    tabs:[
      {
        id:0,
        value:"单曲",
        isActive:true
      },
      {
        id:1,
        value:"歌手",
        isActive:false
      },
      {
        id:2,
        value:"专辑",
        isActive:false
      },
      {
        id:3,
        value:"歌单",
        isActive:false
      },
      {
        id:4,
        value:"MV",
        isActive:false
      },
      {
        id:5,
        value:"主播电台",
        isActive:false
      },
      {
        id:6,
        value:"用户",
        isActive:false
      }
    ],
    //用户输入的值
    inpValue:"",
    //激活索引
    index:0,
    //单曲
    searchSongInfo:[],
    //歌手
    searchSongerInfo:[],
    //专辑
    searchAlbumsInfo:[],
    //歌单
    searchListInfo:[],
    //用户
    searchUserInfo:[]
  },
  //切换输入值
  handleInput(e){
    const {value}=e.detail;
    this.setData({
      inpValue:value
    })
    if(!value){
      this.empty();
    }
  },
  empty(){
    this.setData({
      inpValue:'',
      searchSongInfo:[],
      searchSongerInfo:[],
      searchAlbumsInfo:[],
      searchListInfo:[],
      searchUserInfo:[]
    });
  },
  //逻辑判断
  getSearchInfo(value){
    //获取输入内容和激活选项
    const {inpValue}=this.data;
    let {index}=this.data;
    //当用户有输入才发送请求
    if(inpValue){
      if(index==0){
        //歌单没有数据的时候或者下拉的时候发送请求
        if(this.data.searchSongInfo.length==0||value){
          this.getSearchSongInfo();
        }
      }
      else if(index==1){
        if(this.data.searchSongerInfo.length==0||value){
          this.getSearchSongerInfo();
        }
      }
      else if(index==2){
        if(this.data.searchAlbumsInfo.length==0||value){
          this.getSearchAlbumsInfo()
        }
      }
      else if(index==3){
        if(this.data.searchListInfo.length==0||value){
          this.getSearchListInfo()
        }
      }
      else if(index==6){
        if(this.data.searchUserInfo.length==0||value){
          this.getSearchUserInfo()
        }
      }
    }
    else{
      this.tips('请输入要搜索的内容') 
    }
  },
  //单曲
  async getSearchSongInfo(){
    let {inpValue}=this.data
    const resultInfo=await request({url: 'http://47.106.223.193:3000/v1/search?type=1&limit=&keywords='+inpValue});
    if(resultInfo.code==-460||resultInfo.msg){
      this.setData({
        SearchInfo:resultInfo
      })
    }
    else if(resultInfo.code==400){
      this.tips('没有下一页了');
    }
    else{
      let SearchInfo=resultInfo.result.songs.map(v=>{
        return {
          id:v.id,
          name:v.name,
          author:v.ar[0].name,
          playlistName:v.al.name
        }
      })
      this.setData({
        searchSongInfo:[...this.data.searchSongInfo,...SearchInfo]
      })
    }
  },
  //歌手
  async getSearchSongerInfo(){
    let {inpValue}=this.data
    const resultInfo=await request({url: 'http://47.106.223.193:3000/v1/search?type=100&keywords='+inpValue});
    if(resultInfo.code==-460||resultInfo.msg){
      this.setData({
        SearchInfo:resultInfo
      })
    }
    else if(resultInfo.code==400){
      this.tips('没有下一页了');
    }
    else{
      let SearchInfo=resultInfo.result.artists.map(v=>{
        return {
          picUrl:v.picUrl,
          id:v.id,
          name:v.name,
        }
      })
      this.setData({
        searchSongerInfo:SearchInfo
      })
    }
  },
  //专辑
  async getSearchAlbumsInfo(){
    let {inpValue}=this.data
    const resultInfo=await request({url: 'http://47.106.223.193:3000/v1/search?type=10&keywords='+inpValue});
    if(resultInfo.code==-460||resultInfo.msg){
      this.setData({
        SearchInfo:resultInfo
      })
    }
    else if(resultInfo.code==400){
      this.tips('没有下一页了');
    }
    else{
      let SearchInfo=resultInfo.result.albums.map(v=>{
        return {
          picUrl:v.picUrl,
          id:v.id,
          name:v.name,
          author:v.artist.name
        }
      })
      this.setData({
        searchAlbumsInfo:[...this.data.searchAlbumsInfo,...SearchInfo]
      })
    }
  },
  //歌单
  async getSearchListInfo(){
    let {inpValue}=this.data
    const resultInfo=await request({url: 'http://47.106.223.193:3000/v1/search?type=1000&keywords='+inpValue});
    if(resultInfo.code==-460||resultInfo.msg){
      this.setData({
        SearchInfo:resultInfo
      })
    }
    else if(resultInfo.code==400){
      this.tips('没有下一页了');
    }
    else{
      let SearchInfo=resultInfo.result.playlists.map(v=>{
        return {
          coverImgUrl:v.coverImgUrl,
          id:v.id,
          name:v.name,
          author:v.creator.nickname
        }
      })
      this.setData({
        searchListInfo:[...this.data.searchListInfo,...SearchInfo]
      })
    }
  },
  //用户搜索
  async getSearchUserInfo(){
    let {inpValue}=this.data
    const resultInfo=await request({url: 'http://47.106.223.193:3000/v1/search?type=1002&keywords='+inpValue});
    if(resultInfo.code==-460||resultInfo.msg){
      this.setData({
        SearchInfo:resultInfo
      })
    }
    else if(resultInfo.code==400){
      this.tips();
    }
    else{
      let SearchInfo=resultInfo.result.userprofiles.map(v=>{
        return {
          avatarUrl:v.avatarUrl,
          userId:v.userId,
          name:v.nickname,
          signature:v.signature
        }
      })
      this.setData({
        searchUserInfo:[...this.data.searchUserInfo,...SearchInfo]
      })
    }
  },
  tips(data){
    wx.showToast({
      title: data,
      icon: 'none',
    });
  },
  handleTabsItemChange(e){
    const {index}=e.detail;
    let {tabs}=this.data;
    tabs.forEach((v,i) => i===index?v.isActive=true:v.isActive=false);
    this.setData({
      tabs,
      index
    })
    this.getSearchInfo()
  },
  onReachBottom: function() {
    this.getSearchInfo(true);
  },
})